<?php


namespace app\common\server;


use think\Controller;

class CartServer extends Controller
{
    public function addCart($data)
    {
        $us_id = $data['id'];                           //用户id
        $goods_id = $data['goods_id'];                 //商品id
        $goods_price = $data['goods_price'];           //商品单价
        $goods_norm_id = $data['goods_norm_id'];      //商品规格id
        $goods_norm = $data['goods_norm'];            //商品规格
        $goods_num = $data['goods_num'];              //商品数量
        //$goods_type = $data['goods_type'];          //商品分类
        if (!$goods_id) {
            $this->result('', '0', '请选择商品', 'json');
        }
        if (!$goods_norm) {
            $this->result('', '0', '请选择商品规格', 'json');
        }
        if (!$goods_norm) {
            $this->result('', '0', '请选择商品数量', 'json');
        }
        $map = [
            'pd_id' => $goods_id,
            'us_id' => $us_id,
            'pd_spec' => $goods_norm
        ];
        $cart = model('Cart')->getOne($map);
        if ($cart) {
            $result = model('Cart')->where('id', $cart['id'])->setInc('pd_num', $goods_num);
            if ($result) {
                return $this->result($result, '1', '购物车商品数量+ .$goods_num', 'json');
                //  return $this->s_msg('购物车商品数量+'.$goods_num,$result);
            } else {
                return $this->result('', '0', '添加失败', 'json');
            }
        }

        $goods = model('Product')->where('id', $goods_id)->find();
//        $goods =json($goods);
//        var_dump($goods);exit();
        $carts = [
            'us_id' => $us_id,
            'pd_id' => $goods_id,
            'ca_pid' => $goods['ca_id'], //商品分类
            'pd_spec' => $goods_norm,
            'pd_num' => $goods_num,
            'sku_id' => $goods_norm_id,
            'pd_name' => $goods['pd_name'], //商品名字
            'pd_price' => $goods_price,
            'pd_pic' => $goods['pd_pic'],
            'pd_head_pic' => $goods['pd_head_pic'],
            'pd_content' => $goods['pd_content'],
            'add_time' => date('Y-m-d H:i:s')
        ];
        $rel = model("Cart")->addInfo($carts);
        if ($rel) {
            return $this->result($rel, '1', '添加成功', 'json');
        } else {
            return $this->result('', '0', '添加失败', 'json');
        }


    }

}